<?php

/**
 * Cubi Application Platform
 *
 * LICENSE
 *
 * This source file is subject to the BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @package   openbiz.bin
 * @copyright Copyright (c) 2005-2011, Rocky Swen
 * @license   http://www.opensource.org/licenses/bsd-license.php
 * @link      http://www.phpopenbiz.org/
 * @version   $Id$
 */

/**
 * MetaGenerator class
 *
 * MetaGenerator generate CRUD metadata and template from database table
 *
 * @package   cubi.bin.tools.lib
 * @author    Rocky Swen
 * @copyright Copyright (c) 2005-2010, Rocky Swen
 * @access    public
 */
class MetaGenerator
{

    /**
     * config object
     * @var GenitTableConfig
     */
    public $config;

    /**
     * Package name
     *    example module.package.name
     *    <pre>
     *          module.package.name
     *    </pre>
     * 
     * @var string
     */
    public $packageName;

    /**
     * database name alias in Config.xml
     * 
     * @var string
     */
    public $dbName;

    /**
     * table name
     * 
     * @var string
     */
    public $tableName;

    /**
     * Component name
     * 
     * @var string
     */
    public $compName;

    /**
     * Component description
     * @var string
     */
    public $compDesc;
    public $templateDir;

    /**
     * option
     * @var array
     */
    public $options;
    public $isAutoGenerate;

    /**
     * Database information that stored in Config.xml
     * @var array
     */
    public $dbConfig;

    /**
     * Object that generate Data object metafile
     * @var DOGenerator
     */
    public $doGen;

    /**
     * Object that generate Form object metafile
     * @var FormGenerator
     */
    public $formGen;

    /**
     * Object that generate View object metafile
     * @var ViewGenerator
     */
    public $viewGen;
    public $acl;
    public $dashboard_enable = 0;

    /**
     * Initialize object
     * @param string $packageName module name
     * @param string $dbName database name alias in configuration file
     * @param string $tableName database table name
     * @param array $options more option
     * @return void
     */
    function __construct($packageName = null, $dbName = null, $tableName, $options = null)
    {
        //$this->dbConfig = BizSystem::configuration()->getDatabaseInfo($dbname);
        $this->_loadConfig($tableName);
    }

    private function _loadConfig($tableName)
    {

        $this->config = new GenitTableConfig($tableName);

        $this->packageName = $this->config->getPackageName();

        $this->dbName = $this->config->getDatabaseName();

        $this->dbConfig = BizSystem::configuration()->getDatabaseInfo($this->dbName);

        $this->tableName = $this->config->getTableName();

        $this->compName = $this->config->getComponentName();
        $this->compDesc = $this->config->getComponentDesc();
        $this->templateDir = $this->config->getTemplateDir();

        // $opt[0] : package/module path (module/path/path
        $opt[0] = GenitHelper::packageToPath($packageName);

        // $opt[1] : base Object name
        $opt[1] = $this->compName;

        //$opt[2] : module name / base Object description<br />
        $opt[2] = $this->compDesc;

        // $opt[3] : package/module name (module-name.path.path)
        $opt[3] = $this->packageName;

        $this->options = $opt;

        $this->setACL($this->config->getAccessLevel());

        GenitDebug::ShowMessage(__CLASS__ .'::'.__METHOD__);
        print_r($this);
        
        return;
    }

    /**
     * Generate DataObject metafile
     * @return string file name of data object metafile
     */
    public function genDOMeta()
    {
        $this->doGen = new DOGenerator($this->packageName, $this->dbName, $this->tableName, $this->dbConfig, $this->options);
        $this->doGen->metaGen = $this;
        $this->doGen->prepareData();
        $doFile = $this->doGen->generateAllDOs();

        return $doFile;
    }

    /**
     * Generate Form metafile
     * @return array array that contain file name of Form object metafile
     */
    public function genFormMeta()
    {
        if (!$this->doGen) {
            $this->doGen = new DOGenerator($this->packageName, $this->dbName, $this->tableName, $this->dbConfig, $this->options);
            $this->doGen->prepareData();
        }
        $this->formGen = new FormGenerator($this->packageName, $this->doGen, $this->options);
        $this->formGen->metaGen = $this;
        $formFiles = $this->formGen->generateAllForms();
        return $formFiles;
    }

    /**
     * Generate View object metafile
     * @return string file name of View object metafile
     */
    public function genViewMeta()
    {
        if (!$this->formGen) {
            $this->doGen = new DOGenerator($this->packageName, $this->dbName, $this->tableName, $this->dbConfig, $this->options);
            $this->doGen->metaGen = $this;
            $this->doGen->prepareData();
            $this->formGen = new FormGenerator($this->packageName, $this->doGen, $this->options);
        }
        $viewGen = new ViewGenerator($this->packageName, $this->formGen, $this->options);
        $viewGen->metaGen = $this;
        $viewFile = $viewGen->generateAllViews();
        return $viewFile;
    }

    /**
     * Generate module information metafile
     * @return string file name of module metafile
     */
    public function genModXML()
    {
        GenitDebug::ShowMessage('===============' . __CLASS__ . '::genModXML');
        $modGen = new ModGenerator($this->packageName, $this->options, $this->dashboard_enable);
        $modGen->metaGen = $this;
        $modFile = $modGen->generateMod($this->tableName);
        return $modFile;
    }

    public function modifyModXML()
    {
        GenitDebug::ShowMessage('===============' . __CLASS__ . '::modifyModXML');
        $modGen = new ModGenerator($this->packageName, $this->options, $this->dashboard_enable);
        $modGen->metaGen = $this;
        $modFile = $modGen->modifyMod($this->tableName);
        return $modFile;
    }

    public function genDashboardXML()
    {
        $this->dashboard_enable = 1;
        $dashGen = new DashboardGenerator($this->packageName, $this->options);
        $dashGen->metaGen = $this;
        $modFile = $dashGen->generateFiles($this->tableName);
        return $modFile;
    }

    public function setACL($acl)
    {

        // echo "setACL : [" . $acl ."]";

        $resource = strtolower($this->options[3]); // strtolower($this->opts[3]).'.'.strtolower($this->opts[1]);
        switch ($acl) {
            case 1:
                $this->options['acl'] = array(
                    'access' => $resource . '.Access',
                    'manage' => $resource . '.Manage',
                    'create' => $resource . '.Manage',
                    'update' => $resource . '.Manage',
                    'delete' => $resource . '.Manage',
                    'check' => $resource . '.Check',
                    'approve' => $resource . '.Approve'
                );
                break;
            case 2:
                $this->options['acl'] = array(
                    'access' => $resource . '.Access',
                    'manage' => $resource . '.Manage',
                    'create' => $resource . '.Create',
                    'update' => $resource . '.Update',
                    'delete' => $resource . '.Delete',
                    'check' => $resource . '.Check',
                    'approve' => $resource . '.Approve'
                );
                break;
            case 3:

                $this->options['acl']['resource'] = '';
                $this->options['acl'] = array(
                    'access' => '',
                    'manage' => '',
                    'create' => '',
                    'update' => '',
                    'delete' => '',
                    'check' => '',
                    'approve' => ''
                );
                break;
        }
        $this->options['acl']['option'] = $acl;
        $this->options['acl']['resource'] = $resource;

        print_r($this->options['acl']);
    }

    public function getFieldLabel($fieldName)
    {
        $labelFromConfig = false;
        if (isset($this->config['fieldLabel'])) {
            if (isset($this->config['fieldLabel'][$fieldName])) {
                $label = $this->config['fieldLabel'][$fieldName];
                $labelFromConfig = true;
            }
        }

        if (!$labelFromConfig) {
            $label = str_replace('_', ' ', $fieldName);
            $label = str_u;
        }
    }

    public function getModuleTitle()
    {
        $moduleTitle = null;
        if (isset($this->config->config['moduleTitle'])) {
            $moduleTitle = $this->config->config['moduleTitle'];
        } else {
            $moduleTitle = GenitHelper::capitalize(GenitHelper::getModuleName($this->package_name));
        }

        //echo "==============================================\n";
        //echo $moduleTitle . "\n";
        //echo "==============================================\n";

        return $moduleTitle;
    }

}

?>
